#!/bin/sh

set -eu

# Create folders for storing certs and agent token, they should be available
# to owner ie. stork-agent user.
home_dir=/var/lib/stork-agent
mkdir -p "${home_dir}/certs"
mkdir -p "${home_dir}/tokens"
chmod 700 "${home_dir}/certs"
chmod 700 "${home_dir}/tokens"

addgroup -S stork-agent >/dev/null
adduser -h "${home_dir}" -D -S -H -g "Stork Agent User" -G stork-agent stork-agent >/dev/null

# Add stork-agent user to bind so it can read its config files.
if getent group bind > /dev/null; then
    addgroup stork-agent bind
fi

# Add stork-agent user to kea so it can read its config files.
if getent group _kea > /dev/null; then
    addgroup stork-agent _kea
fi

# Change ownership of all folders and files in home dir to stork-agent user.
chown -R stork-agent "${home_dir}"
